package com.jingdianjichi.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.common.enums.ISDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTyperEnum;
import com.jingdianjichi.subject.domain.convert.MutileSubjectBoConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目的策略类
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{
    @Override
    public SubjectInfoTyperEnum getHandlerType() {
        return SubjectInfoTyperEnum.MULTIPLE;
    }
    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        //多选题目插入
        List<SubjectMultiple> subjectMultipleList=new LinkedList<>();
        Preconditions.checkNotNull(subjectInfoBo.getOptionList(),"选项不能为空");
        try {
            subjectInfoBo.getOptionList().forEach(option->{
                SubjectMultiple subjectMultiple = MutileSubjectBoConverter.INSTANCE.convertAnswerBoToMultipleEntity(option);
                subjectMultiple.setSubjectId(subjectInfoBo.getId());
                subjectMultiple.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());
                subjectMultipleList.add(subjectMultiple);
            });
        } catch (Exception e) {
            throw new RuntimeException("多选题集合为空");
        }
        subjectMultipleService.insertBatch(subjectMultipleList);
    }

    @Override
    public SubjectOptionBo query(Integer subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));
        List<SubjectMultiple> result = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBo> subjectAnswerBOList = MutileSubjectBoConverter.INSTANCE.convertEntityToBoList(result);
        SubjectOptionBo subjectOptionBO = new SubjectOptionBo();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;

    }
}
